package lormi.meipin.service.impl;

import lormi.meipin.common.Constants;
import lormi.meipin.common.LMRongYunAPI;
import lormi.meipin.common.SQLJoint;
import lormi.meipin.dto.PositionAddDto;
import lormi.meipin.dto.PositionEditDto;
import lormi.meipin.dto.PositionNearByDto;
import lormi.meipin.dto.PositionSearchDto;
import lormi.meipin.entity.*;
import lormi.meipin.exception.LMException;
import lormi.meipin.service.BaseService;
import lormi.meipin.service.IPositionService;
import lormi.meipin.util.DateUtil;
import lormi.meipin.util.StringUtil;
import lormi.meipin.vo.PositionSearchVo;
import lormi.meipin.vo.PositionSeeVo;
import lormi.meipin.vo.PositionShareSeeVo;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class PositionService extends BaseService implements IPositionService{


	@Override
	public void updatePositionSort(Integer userId) {
		positionMapper.updatePosition(userId);
	}

	@Override
	public void addPosition(PositionAddDto positionAddDto) throws LMException {
		if (positionMapper.selectByMerIdCityIdPCId(positionAddDto.getUserid(), positionAddDto.getCityid(), positionAddDto.getPositionclass())>0) {
			throw new LMException(100032);
		}
		PositionClass pc = positionClassMapper.selectByPrimaryKey(positionAddDto.getPositionclass());
		if (pc==null) {
			throw new LMException(130005);
		}
		int pId = positionMapper.insert(new Position(positionAddDto,pc.getName()));
		if (pId<1) {
			throw new LMException(100035);
		}
		//匹配期望工作，匹配到的推送
		matchingTalent(positionAddDto.getUserid(),pc.getId(),pId,positionAddDto.getCityid());
	}
	
	private void matchingTalent(Integer merId,Integer pcId,Integer pId,Integer cId) throws LMException {
		List<ExpectWork> es = expectWorkMapper.matchingTalent(pcId,cId);
		for(ExpectWork e:es){
			FocusPosition fp = focusPositionMapper.selectByMemIdAndPositionId(e.getMemid(), pId, 3);
			if(fp!=null)continue;
			fp = new FocusPosition();
			fp.setMemid(e.getMemid());
			fp.setMerid(merId);
			fp.setPositionid(pcId);
			fp.setType(3);
			fp.setCreatetime(new Date());
			focusPositionMapper.insert(fp);
			LMRongYunAPI.push(String.valueOf(e.getMemid()),merChantMapper.selectByMerId(merId).getName(),31);
		}
	}

	@Override
	public void delPosition(Integer id) throws LMException {
		if (positionMapper.deleteByPrimaryKey(id)<1) {
			throw new LMException(100026);
		}
	}

	@Override
	public void editPosition(PositionEditDto positionEditDto) throws LMException {
		if (positionMapper.selectByMerIdCityIdPCIdId(positionEditDto.getUserid(), positionEditDto.getCityid(), positionEditDto.getPositionclass(),positionEditDto.getId())>0) {
			throw new LMException(100032);
		}
		PositionClass pc = positionClassMapper.selectByPrimaryKey(positionEditDto.getPositionclass());
		if (pc==null) {
			throw new LMException(130005);
		}
		int pId = positionMapper.updateByPrimaryKey(new Position(positionEditDto,pc.getName()));
		if (pId<1) {
			throw new LMException(100036);
		}
		//匹配期望工作，匹配到的推送
		matchingTalent(positionEditDto.getUserid(),pc.getId(),pId,positionEditDto.getCityid());
	}

	@Override
	public Position getPosition(Integer id) throws LMException {
		return positionMapper.selectByPrimaryKey(id);
	}

	@Override
	public void getHiddenOrShow(Integer id) throws LMException {
		Position position = positionMapper.selectByPrimaryKey(id);
		if (position.getHidden()==0) {
			position.setHidden(1);
		}else {
			position.setHidden(0);
		}
		if (positionMapper.updateByPrimaryKey(position)<1) {
			throw new LMException(100036);
		}
	}

	@Override
	public List<Position> getListPosition(Integer userid) throws LMException {
		return positionMapper.selectListPosition(userid);
	}

	@Override
	public List<PositionSearchVo> getSearchPosition(PositionSearchDto positionSearchDto) throws LMException {
		String sql = SQLJoint.getSearchPositionSql(positionSearchDto);
		int offset = (positionSearchDto.getPagenow()-1)*positionSearchDto.getPagesize();
		sql += " order by p.updatetime desc limit "+offset+", "+positionSearchDto.getPagesize()+";";

		return positionMapper.selectSearchPosition(sql);
	}

	@Override
	public List<PositionSearchVo> getNearByPosition(PositionNearByDto positionNearByDto) throws LMException {

		int offset = (positionNearByDto.getPagenow()-1)*positionNearByDto.getPagesize();
		List<PositionSearchVo> list = positionMapper.selectNearByPosition(positionNearByDto.getLatitude(),positionNearByDto.getLongitude(), 
				offset, positionNearByDto.getPagesize());

		if (list==null || list.size()<1) {
			throw new LMException(100028);
		}
		return list;
	}

	@Override
	public PositionSeeVo getSeePosition(Integer userid, Integer id, String sharecode) throws LMException {
		Position position = null;
		Talent talent ;
		//1. 判断是否是分享请求
		if (sharecode!=null && "SHLF8DF8ASD8".equals(sharecode)) {
			//分享请求没有限制
		}else {
			//如果是个人请求，判断是否已发布期望职位
			if (expectWorkMapper.selectCount(userid)<1) {//没有发布职位的返回异常
				throw new LMException(100020);
			}
			//判断个人是否查看过该职位，没有查看过的推送查看通知
			FocusPosition focusPosition = focusPositionMapper.selectByMemIdAndPositionId(userid, id, 2);//2:表示是查看的类型
			if (focusPosition==null) {
				position = positionMapper.selectByPrimaryKey(id);
				talent = talentMapper.selectByMemId(userid);
				LMRongYunAPI.push(String.valueOf(position.getMerid()),talent.getName(), 22);//21:表示个人查看推送
				//插入查看通知纪录
				focusPositionMapper.insert(new FocusPosition(id,position.getMerid(),userid,2));
			}
		}
		PositionSeeVo positionSeeVo = new PositionSeeVo();
		if (position==null) {
			position = positionMapper.selectByPrimaryKey(id);
		}
		MerChant merChant = merChantMapper.selectByMerId(position.getMerid());
		//查询关注状态
		boolean isfocus = false;
		if(!"SHLF8DF8ASD8".equals(sharecode)){	
			FocusPosition focus = focusPositionMapper.selectByMemIdAndPositionId(userid, id, 1);
			if(focus!=null){
				isfocus=true;
			}
		}
		positionSeeVo.setId(position.getId());
		positionSeeVo.setEnvironments(Constants.getImgs(environmentMapper.selectByMerId(position.getMerid())));
		positionSeeVo.setMerId(position.getMerid());
		positionSeeVo.setHidden(position.getHidden());
		positionSeeVo.setMerphoto(merChant.getPhoto());
		positionSeeVo.setMax(String.valueOf(position.getSalarymax()));
		positionSeeVo.setMin(String.valueOf(position.getSalarymin()));
		positionSeeVo.setUsername(merChant.getName());
		positionSeeVo.setNature(systemInfoMapper.selectByKey("nature_"+merChant.getNature()).getSvalue());
		positionSeeVo.setPosition(merChant.getTitle());
		positionSeeVo.setCompany(merChant.getCompany());
		positionSeeVo.setAddress(merChant.getAddress());
		positionSeeVo.setNeedposition(positionClassMapper.selectByPrimaryKey(position.getPositionclass()).getName());
		positionSeeVo.setExperienceId(position.getExperience());
		positionSeeVo.setCreateDate(DateUtil.formaDateToString(position.getCreatetime(),"yyyy年M月d日"));
		positionSeeVo.setLabels(Constants.getLabel(labelTemplateMapper.selectByList(StringUtil.getSplit(merChant.getLabel(),";"))));
		positionSeeVo.setDescription(position.getDescription());
		positionSeeVo.setIsfocus(isfocus);
		return positionSeeVo;
	}
	
	@Override
	public PositionShareSeeVo getShareSee(Integer userid) throws LMException {
		PositionShareSeeVo positionShareVo = new PositionShareSeeVo();
		
		MerChant merChant = merChantMapper.selectByMerId(userid);
		if(merChant!=null){
			positionShareVo.setUserid(userid);
			positionShareVo.setEnvironments(environmentMapper.selectByMerId(userid));
			positionShareVo.setCompany(merChant.getCompany());
			positionShareVo.setAddress(merChant.getAddress());
			positionShareVo.setMerphoto(merChant.getPhoto());
			positionShareVo.setNature(systemInfoMapper.selectByKey("nature_"+merChant.getNature()).getSvalue());
			positionShareVo.setUsername(merChant.getName());
			positionShareVo.setLabels(labelTemplateMapper.selectByList(StringUtil.getSplit(merChant.getLabel(),";")));
			List<Position> l = positionMapper.selectNotHidden(userid);
			for(Position p:l){
				p.setCityname(cityMapper.selectByPrimaryKey(p.getHostcity()).getName());
			}
			positionShareVo.setPositions(l);
			positionShareVo.setPosition(merChant.getTitle());
		}else{
			throw new LMException(150012);
		}
		return positionShareVo;
	}
	
	
	
	
}